package com.sxl.ocr.ocronnx.config;

import com.benjaminwan.ocrlibrary.OcrEngine;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * description:
 * author: shenxiaolong
 * date: 2021/1/27  14:45
 */

@Data
@Slf4j
@Configuration
@ConfigurationProperties(prefix = "ocr")
public class JavaConfig {

    private Integer numThread;
    private Integer padding;
    private Float boxScoreThresh;
    private Float boxThresh;
    private Float minArea;
    private Float unClipRatio;
    private Boolean doAngle;
    private Boolean mostAngle;
    private String modelsDir;
    private String tmpDir;

    @Bean
    public OcrEngine getOcrEngine() throws JsonProcessingException {
        OcrEngine ocrEngine = new OcrEngine();
        String version = ocrEngine.getVersion();
        log.info("version={}", version);
        ocrEngine.setNumThread(numThread);
        ocrEngine.initLogger(false, false, false);
        boolean ret = ocrEngine.initModels(modelsDir);
        log.info("init Models:{} ", ret);
        ocrEngine.setPadding(padding);
        ocrEngine.setBoxScoreThresh(boxScoreThresh);
        ocrEngine.setBoxThresh(boxThresh);
        ocrEngine.setMinArea(minArea);
        ocrEngine.setUnClipRatio(unClipRatio);
        ocrEngine.setDoAngle(doAngle);
        ocrEngine.setMostAngle(mostAngle);
        log.info("初始化\n{}", new ObjectMapper().writeValueAsString(ocrEngine));
        return ocrEngine;
    }

}
